﻿using System;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Reflection;
namespace Data
{
	 	//LotteryBuyDetail
		public class LotteryBuyDetail
	{	
		/// <summary>
        /// 主键
        /// </summary>
        private readonly string primaryKey= "id";
		/// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Add(Model.LotteryBuyDetail model)
        {
            return new DataAccess().Add<Model.LotteryBuyDetail>(model, primaryKey);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Update(Model.LotteryBuyDetail model)
        {
            return new DataAccess().Update<Model.LotteryBuyDetail>(model, primaryKey);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Delete(long Id)
        {
            return new DataAccess().Delete<Model.LotteryBuyDetail>(Id, primaryKey);
        }
        /// <summary>
        /// 单条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Model.LotteryBuyDetail GetModel(long Id)
        {
            return new DataAccess().Get<Model.LotteryBuyDetail>(Id, primaryKey);
        }
        public Model.LotteryBuyDetail GetModelByLotteryBuyDetailid(long LotteryBuyDetailid)
        {
            return new DataAccess().Get<Model.LotteryBuyDetail>(LotteryBuyDetailid, "LotteryBuyDetailid");
        }
        public Model.LotteryBuyDetail GetModelBysql(string sql)
        {
            return new DataAccess().Get<Model.LotteryBuyDetail>(sql);
        }
        /// <summary>
        /// 总数据数
        /// </summary>
        /// <returns></returns>
        public int GetAll_Count()
        {
            return new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey);
        }
        /// <summary>
        /// 抽取分页数据
        /// </summary>
        public List<Model.LotteryBuyDetail> GetPageList_Where(int PageIndex, int PageSize, long newsclassid, out int totalCount)
        {          
            string strWhere = GetSqlWhere(newsclassid);
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey+" desc", strWhere);
        }
        public List<Model.LotteryBuyDetail> GetIsWin(int PageIndex, int PageSize, long type, out int totalCount)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" IsWin=1"); 
            if (type > 0)
            {
                sqlWhere.Append(" and LotteryType =" + type);
            } 
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, sqlWhere.ToString());
            return new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey + " desc", sqlWhere.ToString());
        }
        public List<Model.LotteryBuyDetail> GetWinUserListByWhere(int PageIndex, int PageSize, int lotteryType, string lotteryQs, out int totalCount)
        {
            string strWhere = GetGetWinUserListSqlWhere(lotteryType, lotteryQs);
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey+" desc", strWhere);
        }
        public List<Model.LotteryBuyDetail> getlistbytypeandqs(int PageIndex, int PageSize, int lotteryType, string lotteryQs, out int totalCount)
        {
            string strWhere = "and LotteryQs =" + lotteryQs + " and LotteryType =" + lotteryType;
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere);
        }
        public List<Model.LotteryBuyDetail> GetPageListWhere(int PageIndex, int PageSize, string lotteryQs, int lotteryType, out int totalCount)
        {          
            string strWhere = GetSqlWhere(lotteryQs,lotteryType);
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey+" desc", strWhere);
        }
        public List<Model.LotteryBuyDetail> GetPageListBysql(int PageIndex, int PageSize, string strWhere, out int totalCount)
        {
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, strWhere);
            return new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere);
        }
        public DataTable GetPageDT_Where(int PageIndex, int PageSize, long newsclassid, out int totalCount)
        {
            string strWhere = GetSqlWhere(newsclassid);
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, strWhere);
            return ConvertHelper<Model.LotteryBuyDetail>.ConvertToDT(new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere));
        }
        public DataTable GetPageDTWhere(int PageIndex, int PageSize, int lotteryType, string lotteryQs, out int totalCount)
        {
            string strWhere = GetSqlWhere(lotteryQs, lotteryType);
            totalCount = new DataAccess().GetCount<Model.LotteryBuyDetail>(primaryKey, strWhere);
            return ConvertHelper<Model.LotteryBuyDetail>.ConvertToDT(new DataAccess().GetPage_List<Model.LotteryBuyDetail>(PageIndex, PageSize, primaryKey, primaryKey + " desc", strWhere));
        }
        /// <summary>
        /// 拼接条件
        /// </summary>
        public string GetSqlWhere(string lotteryQs, int lotteryType)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" 1>0");
            if (lotteryQs != "")
            {
                sqlWhere.Append(" and LotteryQs =" + lotteryQs); 
            }
            if (lotteryType > 0)
            {
                sqlWhere.Append(" and LotteryType =" + lotteryType);
            }         
            return sqlWhere.ToString();
        }
        public string GetGetWinUserListSqlWhere(int lotteryType,string lotteryQs )
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" IsWin=1 and Flag=1");
            if (lotteryQs != "")
            {
                sqlWhere.Append(" and LotteryQs =" + lotteryQs); 
            }
            if (lotteryType > 0)
            {
                sqlWhere.Append(" and LotteryType =" + lotteryType);
            }         
            return sqlWhere.ToString();
        }
        /// <summary>
        /// 拼接条件
        /// </summary>
        public string GetSqlWhere(long newsclassid)
        {
            StringBuilder sqlWhere = new StringBuilder();

            sqlWhere.Append(" 1>0");

            if (newsclassid>0)
            {
                sqlWhere.Append(" and LotteryType =" + newsclassid);
            }         
            return sqlWhere.ToString();
        }


        /// <summary>
        /// 批删
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public int Drop(string Ids)
        {
            return new DataAccess().Drop<Model.LotteryBuyDetail>(Ids, primaryKey);
        }




        public DataTable GetPageListBysql(string sql)
        {
            return DbHelp.ExecuteDataset(DBConfig.ConnectionSettings, CommandType.Text, sql, null).Tables[0];

        }
    }
}